package org.zh.client.config;

import org.sqlite.SQLiteDataSource;
import org.zh.client.tra_aop.LocalTransaction;
import org.zh.client.tra_aop.RemoteTransactionBean;

import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import static org.zh.util.CommonStatic.random;


public class GlobalStatic {

    public static String getGlobal_TX_ID() {
        return UUID.randomUUID().toString() + random.nextInt(1000);
    }

    public static final Map<String, LocalTransaction> LocalTransactionCache = new ConcurrentHashMap<>();
    public static final InheritableThreadLocal<String> GLOBAL_ThreadLocal = new InheritableThreadLocal<>();
    public static final ThreadLocal<Connection> connectionThreadLocal = new ThreadLocal<>();

    public static final String GLOBAL_TRANSACTION = "GLOBAL_TRANSACTION";
    public static final Map<String, List<RemoteTransactionBean>> globalRegistryCache = new HashMap<>();
    public static final Map<String, List<RemoteTransactionBean>> globalErrorCache = new HashMap<>(1);
    public static final String GLOBAL_ROLLBACK_COMMIT = "/zhOpen/rollbackOrCommit";
    public static final String GLOBAL_REGISTRY = "/zhOpen/registry";
    public static final String GLOBAL_ERROR = "/zhOpen/error";

    public static final SQLiteDataSource dataSource = new SQLiteDataSource();
    public static final String sqliteUrl = "jdbc:sqlite:transactionCache.db";

    static {
        dataSource.setUrl(sqliteUrl);
    }
}
